<template>
  <a-card title="流程">
    <div slot="extra" class="drawer-bootom-button">
      <a-button @click="handle_Add()">
        新增
      </a-button>
    </div>
    <div style="background: #fff;">
      <a-col :md="10" :sm="24">
        <template>
          <a-tree
            @select="onSelect"
            @check="onCheck"
            :selectedKeys="selectedKeys"
            :checkedKeys="checkedKeys"
            :treeData="departTree"
            :checkStrictly="checkStrictly"
            :expandedKeys="iExpandedKeys"
            :autoExpandParent="autoExpandParent"
            :replaceFields="{children:'children', title:'modelName', key:'modelCode' }"
            @expand="onExpand"/>
        </template>
      </a-col>
    </div>
    <left-model ref="modalFormOk1" @ok="modalFormOk1"></left-model>
  </a-card>
</template>

<script>
  import {getAction} from '@/api/manage'
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import leftModel from './leftModel'

  export default {
    name: "FlowTree",
    mixins: [JeecgListMixin],
    components: {
      leftModel
    },
    data() {
      return {
        iExpandedKeys: [],
        loading: false,
        autoExpandParent: true,
        currFlowId: '',
        currFlowName: '',
        treeData: [],
        departTree: [],
        hiding: true,
        checkedKeys: [],
        selectedKeys: [],
        currSelected: {},
        allTreeKeys: [],
        checkStrictly: true,
        url: {
          tree: '/process/list'
        },
        orgCategoryDisabled: false,
      }
    },
    computed: {},
    methods: {
      handle_Add: function () {
        this.$refs.modalFormOk1.add();
      },
      modalFormOk1() {
        // 新增/修改 成功时，重载列表
        this.loadData();
      },
      loadData() {
        this.refresh();
      },
      loadTree() {
        var that = this
        that.treeData = []
        that.departTree = []
        getAction(this.url.tree).then((res) => {
          if (res.success) {
            if (res.result.length > 0) {
              if (that.selectedKeys.length === 0) {
                that.selectedKeys = []
                that.selectedKeys.push(res.result[0].modelCode)
                that.$emit('selected', that.selectedKeys)
              }
            }
            //部门全选后，再添加部门，选中数量增多
            this.allTreeKeys = [];
            for (let i = 0; i < res.result.length; i++) {
              let temp = res.result[i]
              that.treeData.push(temp)
              that.departTree.push(temp)
              that.setThisExpandedKeys(temp)
              that.getAllKeys(temp);
              // console.log(temp.id)
            }
            that.loading = false
          }
        })
      },
      setThisExpandedKeys(node) {
        if (node.children && node.children.length > 0) {
          this.iExpandedKeys.push(node.key)
          for (let a = 0; a < node.children.length; a++) {
            this.setThisExpandedKeys(node.children[a])
          }
        }
      },
      refresh() {
        this.loading = true
        this.loadTree()
      },
      onExpand(expandedKeys) {
        console.log('onExpand', expandedKeys)
        this.iExpandedKeys = expandedKeys
        this.autoExpandParent = false
      },
      nodeModalOk() {
        this.loadTree()
      },
      nodeModalClose() {
      },
      onCheck(checkedKeys, info) {
        console.log('onCheck', checkedKeys, info)
        this.hiding = false
        //---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------
        if (this.checkStrictly) {
          this.checkedKeys = checkedKeys.checked;
        } else {
          this.checkedKeys = checkedKeys
        }
        //---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------
      },
      onSelect(selectedKeys, e) {
        this.hiding = false
        let record = e.node.dataRef
        console.log('onSelect-record', record)
        this.currSelected = Object.assign({}, record)
        this.selectedKeys = [record.modelCode]
        this.$emit('selected', this.currSelected)
      },
      getCurrSelectedTitle() {
        return !this.currSelected.title ? '' : this.currSelected.title
      },
      onClearSelected() {
        this.hiding = true
        this.checkedKeys = []
        this.currSelected = {}
        this.selectedKeys = []
        this.$refs.departAuth.departId = ''
      },
      //---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------
      expandAll() {
        this.iExpandedKeys = this.allTreeKeys
      },
      closeAll() {
        this.iExpandedKeys = []
      },
      getAllKeys(node) {
        // console.log('node',node);
        this.allTreeKeys.push(node.key)
        if (node.children && node.children.length > 0) {
          for (let a = 0; a < node.children.length; a++) {
            this.getAllKeys(node.children[a])
          }
        }
      }
      //---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------

    },
    created() {
      this.currFlowId = this.$route.params.id
      this.currFlowName = this.$route.params.name
      // this.loadTree()
    },
  }
</script>

<style lang="less" scoped>
  .ant-card {
    height: 100%;

    /deep/ .ant-card-head {
      padding: 0 15px;

      /deep/ .ant-card-head-title, /deep/ .ant-card-extra {
        padding: 10px 0;
      }
    }

    /deep/ .ant-card-body {
      padding: 10px;
      height: calc(100% - 65px);
      overflow: auto;
    }
  }
</style>